<?php
namespace catchAdmin\externalGoods\model\search;
use catchAdmin\externalGoods\model\Category;
use catchAdmin\externalGoods\service\CategoryService;
use catcher\CatchAuth;
use think\facade\Db;

trait GoodsSearch
{
//    public function searchArbitraryFieldAttr($query, $value, $data){
//        return $query->where('g.brand_name|g.goods_name|goods_keywords','like',"%$value%");
//    }

//    public function searchArbitraryCodeAttr($query, $value, $data){
//        return $query->where('goods_spu|goods_name','like',"%$value%");
//    }

/**数据量大 很卡
    //是否添加
    public function searchIsAddedAttr($query, $value, $data){
        $user = (new CatchAuth())->user();
        $tenant_id = $user['tenant_id'];
        $_v = $value == '已添加' ? 'not null' : 'null';
        return
            $query->leftJoin('db_tenant_goods tg',"tg.syst_goods_id =sg.id and tg.tenant_id = {$tenant_id}  and tg.deleted_at = 0")
            ->where('tg.syst_goods_id',$_v)
            ;
    }
 *
 */

    public function searchIsAddedAttr($query, $value, $data){
        $user = (new CatchAuth())->user();
        $tenant_id = $user['tenant_id'];
        $_v = $value == '已添加' ? 'not null' : 'null';
        return
            $query->leftJoin('db_tenant_goods tg',"tg.syst_goods_id =sg.id and tg.tenant_id = {$tenant_id}  and tg.deleted_at = 0")
                ->where('tg.syst_goods_id',$_v)
            ;
    }

//    // 限制ID
//    public function searchIdFiltrationAttr($query, $value, $data){
//        if(is_array($value) && count($value) == 2){
//            return $query->where('sg.id',$value[0],$value[1]);
//        }
//    }

    public function searchGoodsSpuAttr($query, $value, $data){
//        return $query->whereLike('sg.goods_spu',$value);
        return $query->where('sg.goods_spu',$value);
    }

    //是否上架
    public function searchIsSaleAttr($query, $value, $data){
        return $query->where('sg.is_sale', $value);
    }


    //品牌名
    public function searchBrandNameAttr($query, $value, $data){
        //return $query->where('brand_name','like',"%$value%");
        return $query->whereLike('sg.brand_name', $value);
    }

    //商品名
    public function searchGoodsNameAttr($query, $value, $data){
        return $query->whereLike('sg.goods_name', $value);
    }

    //贸易类型
    public function searchTradeTypeNameAttr($query, $value, $data){
        return $query->where('sg.trade_type_name', $value);
    }

    //商品分类
    public function searchCategoryIdAttr($query, $value, $data)
    {
        $categoryModel = new Category();
        //$h_id = $categoryModel->where('id',$value)->value('cate_id');
        $h_id = $value;

        //取出所有下级分类
        $c_id = explode(',', (new CategoryService())->get_all_child_cate($h_id));
        $c_id[] = $h_id;

        $c_id = array_values(array_filter($c_id));

        return $query->where('sg.category_id','in', $c_id);
    }

    //选择供应商
    public function searchSupplierIdAttr($query, $value, $data){
        return $query->where('sg.supplier_id', $value);
    }
}